package com.zengguang.testkaoshi.controller;

import com.zengguang.testkaoshi.pojo.Personinfo;
import com.zengguang.testkaoshi.services.impl.PersoninfoService;
import com.zengguang.testkaoshi.tool.ResponseMessage;
import com.zengguang.testkaoshi.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "personinfo")
@RestController
@CrossOrigin
public class PersoninfoAction {
    @Autowired
    private PersoninfoService personinfoService;

    @ApiOperation(value = "人员信息添加方法")
    @RequestMapping(value = "/addPersonInfo",method = RequestMethod.GET)
    public ResponseMessage addPersonInfo(Personinfo personinfo) {
        int isAdd = 0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (personinfo != null) {
            //添加角色数据
            isAdd = personinfoService.insertPersoninfo(personinfo);
            if (isAdd == 1) {
                return Result.success("添加成功");
            } else {
                return Result.error("添加失败");
            }
        }
        return Result.error("添加失败");
    }

    @ApiOperation(value = "人员信息修改方法")
    @RequestMapping(value = "/updatePersonInfo",method = RequestMethod.GET)
    public ResponseMessage updatePersonInfo(Personinfo personinfo) {
        int isUpadte = 0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (personinfo != null) {
            //添加角色数据
            isUpadte = personinfoService.updatePersoninfo(personinfo);
            if (isUpadte >0) {
                return Result.success("人员信息更新成功");
            } else {
                return Result.error("人员信息更新失败");
            }

        }
        return Result.error("人员信息更新失败");
    }

    @ApiOperation(value = "人员信息查询方法")
    @RequestMapping(value = "/selectAllPersonInfo",method = RequestMethod.GET)
    public ResponseMessage selectAllPersonInfo(Personinfo personinfo) {
        Map<String, List> map=new HashMap<>();
        List<Personinfo> list=personinfoService.selectAllPersoniinfo();
        if (!list.isEmpty()){
            map.put("list",list);
            return Result.success("200",map);
        }
        return Result.error("查询失败");
    }

    @ApiOperation("删除人员信息")
    @RequestMapping(value = "/deletePeopleInfo",method = RequestMethod.GET)
    public ResponseMessage deletePeopleInfo(int peopleinfoid){
        int temp=0;
        if (peopleinfoid!=0){
            temp=personinfoService.deletePersoninfo(peopleinfoid);
            if (temp>0) {
                return Result.success("删除人员信息成功",temp);
            }
            else {
                return Result.error("删除人员信息失败");
            }
        }

        return Result.error("人员信息编号不为0");
    }

    @ApiOperation(value = "根据用户名和密码查询人员信息方法")
    @RequestMapping(value = "/selectByIDandPwd",method = RequestMethod.GET)
    public ResponseMessage selectByIDandPwd(String loginid,String pwd) {
        Map<String, List> map=new HashMap<>();
        List<Personinfo> list=personinfoService.selectByIDandPwd(loginid,pwd);
        if (!list.isEmpty()){
            map.put("list",list);
            return Result.success("200",map);
        }
        return Result.error("查询失败，用户名或密码错误");
    }
}
